home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / filevirus26alib.lha / examples / fvkiller.c < prev    next >
C/C++ Source or Header  |  1993-03-30  |  3KB  |  133 lines

  1. /* ----------------------------------------------------------------
  2.  *
  3.  *  Project: Filevirus Library
  4.  *
  5.  *  Program: fvkiller.c
  6.  *
  7.  *  Author : Bjorn Reese <breese@imada.ou.dk>
  8.  *
  9.  *  Short  : Example of a simple virus killer
  10.  *           Compile with "sc LINK fvkiller.c"
  11.  *
  12.  * ---------------------------------------------------------------- */
  13.  
  14. #include <stdio.h>
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <dos/dos.h>
  19. #include <dos/dostags.h>
  20. #include <proto/exec.h>
  21. #include <proto/dos.h>
  22.  
  23. #include <libraries/filevirus.h>
  24. #include <proto/filevirus.h>
  25.  
  26. extern struct Library *SysBase;
  27. struct FilevirusBase *FilevirusBase=NULL;
  28.  
  29. /* ---------------------------------------------------------------- */
  30.  
  31. long FileSize(BPTR file)
  32. {
  33.   long len = 0;
  34.   struct FileInfoBlock *fib;
  35.  
  36.   if ( fib=AllocDosObjectTags(DOS_FIB, ADO_FH_Mode) ) {
  37.     len = ExamineFH(file, fib) ? fib->fib_Size : 0L;
  38.     FreeDosObject(DOS_FIB, fib);
  39.   }
  40.   return len;
  41. }
  42.  
  43. /* ---------------------------------------------------------------- */
  44.  
  45. void RepairFile(struct FilevirusNode *p, char *fname)
  46. {
  47.   BPTR f = NULL;
  48.   long len;
  49.   void *buff;
  50.   char answer[256];
  51.  
  52.   if ( f=Open(fname, MODE_OLDFILE) ) {
  53.  
  54.     len = FileSize(f);
  55.     if (buff = AllocMem((ULONG)len, MEMF_ANY|MEMF_CLEAR) ) {
  56.  
  57.       Read(f, buff, len);
  58.       p->fv_Buffer = buff;
  59.       p->fv_BufferLen = (ULONG)len;
  60.  
  61.       printf("File: '%s' ", fname);
  62.  
  63.       if ( !fvCheckFile(p, 0) ) {
  64.  
  65.         if (p->fv_FileInfection != NULL) {
  66.  
  67.           printf("*** virus '%s' found\n", (p->fv_FileInfection)->fi_VirusName);
  68.           printf("Repair (yes/no) : ");
  69.           fgets(answer, 256, stdin);
  70.  
  71.           if (answer[0] == 'y') {
  72.  
  73.             switch (fvRepairFile(p, NULL, 0)) {
  74.  
  75.               case FVMSG_DELETE:
  76.                 Close(f);
  77.                 f = NULL;
  78.                 if ( DeleteFile(fname) )
  79.                   printf("File deleted\n");
  80.                 else
  81.                   printf("Error! Cannot delete\n");
  82.                 break;
  83.  
  84.               case FVMSG_RENAME:
  85.                 printf("Please rename\n");
  86.                 break;
  87.  
  88.               case FVMSG_SAVE:
  89.                 Close(f);
  90.                 if ( f=Open(fname, MODE_NEWFILE) ) {
  91.                   Seek(f, 0, OFFSET_BEGINNING);
  92.                   Write(f, buff, (int)p->fv_BufferLen);
  93.                   printf("Hunk/code removed\n");
  94.                 } else
  95.                   printf("Error! Unable to remove hunk/code\n");
  96.                 break;
  97.  
  98.               default:
  99.                 printf("No action taken\n");
  100.                 break;
  101.             }
  102.           }
  103.         } else printf("clean\n");
  104.  
  105.       } else printf("error %d\n", p->fv_Status);
  106.  
  107.       FreeMem(buff, len);
  108.     }
  109.     if (f) Close(f);
  110.   }
  111. }
  112.  
  113. /* ---------------------------------------------------------------- */
  114.  
  115. int main(int argc, char *argv[])
  116. {
  117.   struct FilevirusNode *p;
  118.  
  119.   if ( FilevirusBase = (struct FilevirusBase *)OpenLibrary("filevirus.library", 2) ) {
  120.  
  121.     if ( p = fvAllocNode() ) {
  122.  
  123.       RepairFile(p, argv[1]);
  124.       fvFreeNode(p);
  125.  
  126.     } else fprintf(stderr, "fvAllocNode() failed\n");
  127.  
  128.     CloseLibrary((struct Library *)FilevirusBase);
  129.  
  130.   } else fprintf(stderr, "Cannot open 'filevirus.library'\n");
  131.  
  132. }
  133.